﻿using Coding4Fun.Toolkit.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace MultiPhotoChooser
{
    public class MultiPhotoChooserTask : ChooserBase<MultiPhotoResult>
    {
        Popup popup;
        MediaLibraryPage mediaContentPage;
        public bool IsOpen = false;
        public int SelectionLimit = 0;
        public double Width = 480;
        public double Height = 800;

        public delegate void OnCompleteEventHandler(object sender, MultiPhotoResult e);

        private OnCompleteEventHandler _OnComplete;
        public event OnCompleteEventHandler OnComplete
        {
            add
            {
                if (_OnComplete == null || !_OnComplete.GetInvocationList().Contains(value))
                {
                    _OnComplete += value;
                }
            }
            remove
            {
                _OnComplete -= value;
            }
        }

        protected virtual void OnSelectComplete(MultiPhotoResult e)
        {
            if(_OnComplete != null)
                _OnComplete(this, e);

            this.Close();
        }

        public class Container
        {
            public MediaLibraryPage.OnCompleteEventHandler  Event { get; set; }
            public int SelectionLimit { get; set; }
            public double Width { get; set; }
            public double Height { get; set; }
        }

        public override void Show()
        {
            if (popup == null)
                popup = new Popup();

            popup.Width = this.Width;
            popup.Height = this.Height;

            this.IsOpen = true;
            popup.IsOpen = true;

            if(mediaContentPage == null)
                mediaContentPage = new MediaLibraryPage();

            var layout = (Grid)mediaContentPage.FindName("LayoutRoot");
            mediaContentPage.SelectionLimit = this.SelectionLimit;
            popup.Child = mediaContentPage;
            mediaContentPage.SelectComplete += content_SelectComplete;

            popup.VerticalOffset = (Application.Current.Host.Content.ActualHeight - layout.Height) / 2;
            popup.HorizontalOffset = (Application.Current.Host.Content.ActualWidth - layout.Width) / 2;
        }
        
        private void content_SelectComplete(object sender, MultiPhotoResult e)
        {
            OnSelectComplete(e);
        }

        public void Close()
        {
            var p = mediaContentPage.FindName("pivot") as Pivot;

            if (p.SelectedIndex == 1)
            {
                p.SelectedIndex = 0;
                return;
            }
            else if (p.SelectedIndex == 2)
            {
                p.SelectedIndex = 1;
                return;
            }

            this.IsOpen = false;
            popup.IsOpen = false;
            popup = null;

            mediaContentPage.SelectComplete -= content_SelectComplete;
            mediaContentPage = null;
        }

        
    }
}
